load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg/sql/colexecop:EXECGEN.bzl", "eg_go_filegroup", "gen_eg_go_rules")
load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test")

go_library(
    name = "colexecwindow",
    srcs = [
        "buffered_window.go",
        "count_rows_aggregator.go",
        "min_max_queue.go",
        "partitioner.go",
        "window_functions_util.go",
        ":gen-exec",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecwindow",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/col/coldata",  # keep
        "//pkg/col/coldataext",  # keep
        "//pkg/col/typeconv",  # keep
        "//pkg/sql/colcontainer",  # keep
        "//pkg/sql/colconv",  # keep
        "//pkg/sql/colexec/colexecagg",  # keep
        "//pkg/sql/colexec/colexecbase",
        "//pkg/sql/colexec/colexecutils",  # keep
        "//pkg/sql/colexec/execgen",  # keep
        "//pkg/sql/colexecerror",  # keep
        "//pkg/sql/colexecop",  # keep
        "//pkg/sql/colmem",  # keep
        "//pkg/sql/execinfra",  # keep
        "//pkg/sql/execinfra/execreleasable",  # keep
        "//pkg/sql/execinfrapb",  # keep
        "//pkg/sql/memsize",  # keep
        "//pkg/sql/rowenc",  # keep
        "//pkg/sql/sem/builtins",  # keep
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",  # keep
        "//pkg/sql/types",  # keep
        "//pkg/util/duration",  # keep
        "//pkg/util/json",  # keep
        "//pkg/util/mon",  # keep
        "//pkg/util/timeutil/pgdate",  # keep
        "@com_github_cockroachdb_apd_v3//:apd",  # keep
        "@com_github_cockroachdb_errors//:errors",  # keep
        "@com_github_marusama_semaphore//:semaphore",  # keep
    ],
)

go_test(
    name = "colexecwindow_test",
    srcs = [
        "inject_setup_test.go",
        "main_test.go",
        "min_max_queue_test.go",
        "window_framer_test.go",
        "window_functions_test.go",
    ],
    embed = [":colexecwindow"],
    deps = [
        "//pkg/col/coldata",
        "//pkg/col/coldataext",
        "//pkg/settings/cluster",
        "//pkg/sql/catalog/catenumpb",
        "//pkg/sql/colcontainer",
        "//pkg/sql/colexec/colbuilder",
        "//pkg/sql/colexec/colexecagg",
        "//pkg/sql/colexec/colexecargs",
        "//pkg/sql/colexec/colexectestutils",
        "//pkg/sql/colexec/colexecutils",
        "//pkg/sql/colexecerror",
        "//pkg/sql/colexecop",
        "//pkg/sql/colmem",
        "//pkg/sql/execinfra",
        "//pkg/sql/execinfrapb",
        "//pkg/sql/randgen",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sem/tree/treewindow",
        "//pkg/sql/types",
        "//pkg/testutils/colcontainerutils",
        "//pkg/testutils/skip",
        "//pkg/util/encoding",
        "//pkg/util/leaktest",
        "//pkg/util/log",
        "//pkg/util/mon",
        "//pkg/util/randutil",
        "@com_github_cockroachdb_apd_v3//:apd",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_marusama_semaphore//:semaphore",
        "@com_github_stretchr_testify//require",
    ],
)

# Map between target name and relevant template.
targets = [
    ("first_value.eg.go", "first_last_nth_value_tmpl.go"),
    ("lag.eg.go", "lead_lag_tmpl.go"),
    ("last_value.eg.go", "first_last_nth_value_tmpl.go"),
    ("lead.eg.go", "lead_lag_tmpl.go"),
    ("min_max_removable_agg.eg.go", "min_max_removable_agg_tmpl.go"),
    ("nth_value.eg.go", "first_last_nth_value_tmpl.go"),
    ("ntile.eg.go", "ntile_tmpl.go"),
    ("range_offset_handler.eg.go", "range_offset_handler_tmpl.go"),
    ("rank.eg.go", "rank_tmpl.go"),
    ("relative_rank.eg.go", "relative_rank_tmpl.go"),
    ("row_number.eg.go", "row_number_tmpl.go"),
    ("window_aggregator.eg.go", "window_aggregator_tmpl.go"),
    ("window_framer.eg.go", "window_framer_tmpl.go"),
    ("window_peer_grouper.eg.go", "window_peer_grouper_tmpl.go"),
]

# Define a file group for all the .eg.go targets.
eg_go_filegroup(
    name = "gen-exec",
    targets = targets,
)

# Define gen rules for individual eg.go files.
gen_eg_go_rules(targets)

disallowed_imports_test(
    "colexecwindow",
    [
        "//pkg/sql/colexec",
        "//pkg/sql/colexec/colexechash",
        "//pkg/sql/colexec/colexecjoin",
        "//pkg/sql/colexec/colexecproj",
        "//pkg/sql/colexec/colexecprojconst",
        "//pkg/sql/colexec/colexecsel",
    ],
)
